<?php
/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2017/6/15
 * Time: 上午10:25
 */

namespace app\src\seller\action;


use app\src\base\action\BaseAction;
use app\src\base\helper\PageHelper;
use app\src\seller\logic\SellerLogic;

class SellerAction extends BaseAction
{
    /**
     * 查询附近商家
     * @param $lng
     * @param $lat
     * @param bool $maxDistance 距离（单位米）
     * @param bool $name
     * @param PageHelper $PageHelper
     * @return array
     */
    public function getNearby($lng, $lat, $maxDistance = false, $name = false, $city_code = false, PageHelper $pageHelper,$cate_id)
    {
        $result = (new SellerLogic)->getNearby($lng, $lat, $maxDistance, $name, $city_code, $pageHelper,$cate_id);
        if(!$result['status']) return $this->error($result['info']);
        $list = [];
        foreach ($result['info']['list'] as $seller){
            $list[] = $this->sellerInfoFilter($seller);
        }
        $result['info']['list'] = $list;
        return $this->success($result['info']);
    }

    /**
     * 商家详情
     * @param $seller_id
     * @return array
     */
    public function detailInfo($seller_id)
    {
        $result = (new SellerLogic)->detailInfo($seller_id);
        if(!$result['status']) return $this->error($result['info']);
        $seller = $result['info']['seller'];
        $promotions = $result['info']['promotion'];

        $info = [
            'seller' => $this->sellerInfoFilter($seller),
            'promotion' => []
        ];

        unset($info['seller']['distance']);

        foreach ($promotions as $value){
            $info['promotion'][] = $this->promotionFilter($value);
        }

        return $this->success($info);
    }

    /**
     * 商家优惠过滤信息返回
     * @param $promotion
     * @return array
     */
    protected function promotionFilter($promotion)
    {
        return [
            'id' => $promotion['id'],
            'title' => $promotion['title'],
            'detail' => $promotion['detail'],
            'img' => $promotion['img']
        ];
    }

    /**
     * 商家过滤信息返回
     * @param $seller_info
     * @return array
     */
    protected function sellerInfoFilter($seller_info)
    {
        return [
            'id' => $seller_info['id'],
            'name' => $seller_info['name'],
            'price' => $seller_info['price'],
            'grade' => ceil($seller_info['grade']/$seller_info['gn']),
            'position' => $seller_info['position'],
            'lng' => $seller_info['lng'],
            'lat' => $seller_info['lat'],
            'logo' => $seller_info['logo'],
            'main_img' => $seller_info['main_img'],
            'main_img_small' => $seller_info['main_img_small'],
            'contact_number' => $seller_info['contact_number'],
            'synopsis' => $seller_info['synopsis'],
            'first_offer' => $seller_info['first_offer'],
            'monthly_sales' => $seller_info['monthly_sales'],
            'distance' => isset($seller_info['distance']) ? $seller_info['distance'] : 0,
            'category_id' => $seller_info['category_id'],
            'discount_rate' => $seller_info['discount_rate'],
            'category_level' => $seller_info['category_level'],
            'detail' => !empty($seller_info['detail']) ? json_decode($seller_info['detail']) : ['null'=>'no']
        ];
    }

    /**
     * 所有下级商家
     * @param $platform_id
     * @return array
     */
    public function childAll($platform_id)
    {
        $result = (new SellerLogic)->childAll($platform_id);
        if(!$result['status']) return $this->error($result['info']);

        $list = [];
        foreach ($result['info'] as $seller){
            $list[] = $this->sellerInfoFilter($seller);
        }
        return $this->success($list);
    }
}